#!/bin/bash
#

echo "android ndk: $ANDROID_NDK"
echo "toolchain file: $CMAKE_TOOLCHAIN_FILE"
echo "make program: $CMAKE_MAKE_PROGRAM"
echo "android api: $ANDROID_API"
echo "android abi: $ANDROID_ABI"

build="build_$ANDROID_ARCH"

rm -rf "$build"
mkdir -p "$build"
pushd "$build"

cmake  -DCMAKE_TOOLCHAIN_FILE="$CMAKE_TOOLCHAIN_FILE" \
       -DCMAKE_ANDROID_NDK="$ANDROID_NDK" \
       -DCMAKE_BUILD_TYPE=Release \
       -DCMAKE_SYSTEM_NAME="Android" \
       -DCMAKE_SYSTEM_VERSION="$ANDROID_API" \
       -DANDROID_PLATFORM="android-$ANDROID_API" \
       -DANDROID_ABI="$ANDROID_ABI" \
       -DANDROID_NDK="$ANDROID_NDK" \
       -DANDROID_STL="c++_shared" \
       -DCMAKE_CXX_FLAGS="-std=c++11 -frtti -fexceptions -stdlib=libc++ -DUSE_ZLIB" \
       -DOPENGL_PROFILE="GLES3" \
       -DDYNAMIC_OPENTHREADS=OFF \
       -DDYNAMIC_OPENSCENEGRAPH=OFF \
       -G Ninja \
       -DCMAKE_MAKE_PROGRAM="$CMAKE_MAKE_PROGRAM" \
       ..

cmake --build . --target install
error_code=$?
echo "error code: $error_code"

popd
rm -rf "$build"

exit $error_code
